package com.base.controller;

import com.base.po.voip_coupon.CouponGoods;
import com.base.service.COUPON.CouponBrandService;
import com.base.service.COUPON.CouponGoodsService;
import com.base.utils.Page;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 365优惠券商品管理服务端
 *
 * @author K
 */
@Controller
@RequestMapping("/admin")
public class Index_CouponGoods {
    @Autowired
    private CouponBrandService couponBrandService; // 分类信息
    @Autowired
    private CouponGoodsService couponGoodsService;

    /**
     * 商品首页
     *
     * @param request       参数
     * @param brandId       品牌Id
     * @param currentPageNo 当前页
     * @return ModelAndView
     * @throws Exception
     */
    @RequestMapping("/couponGoodsIndex")
    public ModelAndView couponGoodsIndex(HttpServletRequest request, String brandId, Integer currentPageNo) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        page.setCurrentPageNo(currentPageNo); // 设置当前页
        page.setRecordTotal(couponGoodsService.countAll(userid, brandId)); // 设置总条数
        List<CouponGoods> goodses = couponGoodsService.selectAll(page, userid, brandId); // 分类内容集合
        for (CouponGoods goods : goodses){
            goods.setBrandid(couponBrandService.selectByPrimaryKey(goods.getBrandid()).getCategory());
        }
        map.put("goodses", goodses); // 对象
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/index_CouponGoods", map);
    }

    // 跳转添加商品信息
    @RequestMapping("/addCouponGoods")
    public ModelAndView addCouponGoods(HttpServletRequest request) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        Map<String, Object> map = new HashedMap();
        map.put("brands", couponBrandService.selectAll(null, userid, null, null, 1));
        return new ModelAndView("jsp/add_CouponGoods", map);
    }

    // 跳转修改商品信息
    @RequestMapping("/updateCouponGoods")
    public ModelAndView updateCouponGoods(String id) throws Exception {
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        map.put("goods", couponGoodsService.selectByPrimaryKey(id)); // 对象
        return new ModelAndView("jsp/update_CouponGoods", map);
    }
}
